package com.mall.controller;

import com.mall.commons.common.CommonResult;
import com.mall.model.domain.PmsProductCategory;
import com.mall.model.dto.ProductCategoryDto;
import com.mall.model.vo.PageResult;
import com.mall.model.vo.ProductCategoryVo;
import com.mall.service.ProductCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/productCategory")
@Slf4j
public class ProductCategoryController {
    @Autowired
    private ProductCategoryService productCategoryService;

    /**
     * 添加商品分类
     * @param productCategoryDto
     * @return
     */
    @PostMapping("/create")
    public CommonResult creat(@RequestBody ProductCategoryDto productCategoryDto){
        productCategoryService.save(productCategoryDto);
        return CommonResult.success(null,"添加成功");
    }

    /**
     * 删除商品分类
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public  CommonResult delete(@PathVariable Integer id){
        productCategoryService.deleteById(id);
        return CommonResult.success(null,"删除成功");
    }

    /**
     * 查询所有一级分类及子分类
     * @return
     */
    @GetMapping("/list/withChildren")
    public CommonResult list(){
      List<ProductCategoryVo>voList = productCategoryService.selectList();
      return CommonResult.success(voList);
    }

    /**
     * 分页查询商品分类
     * @param parentId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list/{parentId}")
    public CommonResult page(@PathVariable Integer parentId,Integer pageNum,Integer pageSize){
       PageResult pr= productCategoryService.selectByPage(parentId,pageNum,pageSize);
       return CommonResult.success(pr);
    }

    /**
     * 修改导航栏显示状态
     * @param ids
     * @param navStatus
     * @return
     */
    @PostMapping("/update/navStatus")
    public CommonResult navStatus(@RequestParam List<Integer> ids,Integer navStatus){
        productCategoryService.updateNavStatus(ids,navStatus);
        return CommonResult.success("更新成功");
    }

    /**
     * 修改显示状态
     * @param ids
     * @param showStatus
     * @return
     */
    @PostMapping("/update/showStatus")
    public CommonResult showStatus(@RequestParam List<Integer> ids,Integer showStatus){
        productCategoryService.updateShowStatus(ids,showStatus);
        return CommonResult.success("更新成功");
    }

    /**
     * 修改商品分类
     * @param id
     * @param productCategoryDto
     * @return
     */
    @PostMapping("/update/{id}")
    public CommonResult update(@PathVariable Integer id,@RequestBody ProductCategoryDto productCategoryDto){
        productCategoryService.updateProduct(id,productCategoryDto);
        return CommonResult.success("修改成功");
    }

    /**
     * 根据id获取商品分类
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public CommonResult selectById(@PathVariable Integer id){
     PmsProductCategory pmsProductCategory= productCategoryService.selectById(id);
        return CommonResult.success(pmsProductCategory);
    }


}
